package animalParadise;

import java.util.Scanner;

/*
 * 动物管理类
 */

public class AnimalMgr {
	// 动物数组
	Animal[] animals = new Animal[3];
	// 用户选择
	int choice;
	Scanner input = new Scanner(System.in);

	// 初始化动物信息
	public void init() {
		Cat cat = new Cat("加菲猫", 4);
		Duck duck = new Duck("唐小鸭", 2);
		Dolphin dolphin = new Dolphin("海豚奇奇");
		animals[0] = cat;
		animals[1] = duck;
		animals[2] = dolphin;
	}

	// 显示所有动物相关信息
	public void info() {
		System.out.println("动物名字\t\t腿的条数\t\t动物叫");
		for (Animal animal : animals) {
			String name = animal.getName();
			int legNum = 0;
			if (animal instanceof Terrestrial) {
				legNum = ((Terrestrial) animal).getLegNum();
			}
			String legNums = (legNum == 0) ? "" : (legNum + "");
			String shout = animal.shout();
			System.out.println(name + "\t\t" + legNums + "\t\t" + shout);
		}
		System.out.println("是否要继续修改数据：按0进行修改操作，其他任意数字键退出");
		choice = input.nextInt();
		if(choice == 0){
			try{
				modify();
			}catch(Exception ex){
				ex.printStackTrace();
			}finally{
				info();
			}
		} else {
			System.out.println("谢谢使用，程序结束！");
			System.exit(1);
		}
	}
	
	// 修改动物信息
	public void modify() throws Exception{
				String name;
				int legNum;
				System.out.print("请输入猫的名字：");
				name = input.next();
				System.out.print("请输入猫腿的条数：");
				legNum = input.nextInt();
				if(legNum != 4){
					throw new Exception("猫只能有4条腿");
				}
				animals[0] = new Cat(name,legNum);
				System.out.print("请输入鸭的名字：");
				name = input.next();
				System.out.print("请输入鸭腿的条数：");
				legNum = input.nextInt();
				if(legNum != 2){
					throw new Exception("鸭只能有2条腿");
				}
				animals[1] = new Duck(name,legNum);
				System.out.print("请输入海豚的名字：");
				name = input.next();
				animals[2] = new Dolphin(name);
				info();
	}
}
